﻿////////////////////////////////////////////////////////////////////
// © Copyright 2007 NeoGeo New Media GmbH (http://www.neogeo.com) //
////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////
// helper variables for the zoom levels ***********************
// (variables for coordinating zoomdepths)
///////////////////////////////////////////////////////////////
var ZoomLevel = 2.0;
var ZoomLoad = 1.8;
var ZoomDepth = 0;
var MaxDepth = 0;

///////////////////////////////////////////////////////////////
// global zoomlevel ***************************************
// (returns the current zoom-level as float)
///////////////////////////////////////////////////////////////
function Zoom()
{
    var sceneScale = MainRootElement.findName("sceneScale");
    var ZoomFactor = 1;
    ZoomFactor = Math.round(sceneScale.scaleX * 100);
    ZoomFactor = ZoomFactor / 100;
    return ZoomFactor;
}

///////////////////////////////////////////////////////////////
// specific zoom **********************************************
// (zooms in, either main- or overview canvas)
///////////////////////////////////////////////////////////////
function TargetZoomIn(overview)
{
    mousePosition();
    var sceneScale = MainRootElement.findName("sceneScale");
    if(!overview)
    {
        if(Zoom() <= 1.3)
        {
            sceneScale.centerX = MousePosX;
            sceneScale.centerY = MousePosY;
        }
    }
    else
    {
        sceneScale.centerX = (MousePosX - OverviewLeftSpacing) * 4 + LeftSpacing;
        sceneScale.centerY = (MousePosY - OverviewTopSpacing) * 4 + TopSpacing;
    }
    
    // call to the global progressiv zooming function
    ZoomIn();
}

///////////////////////////////////////////////////////////////
// overview refresh *******************************************
// (refreshes the current zoomlevel of the overview canvas)
///////////////////////////////////////////////////////////////
function OverviewZoom()
{
    var Selection = MainRootElement.findName("Selection");
    
    if (Zoom() <= 1.0) {
        Selection.Width = OverviewSizeX;
        Selection.Height = OverviewSizeY;
    }    
    else {
        Selection.Width = Math.round(OverviewSizeX / Zoom());
        Selection.Height = Math.round(OverviewSizeY / Zoom());
    }
    // call to the overview position refresh function
    OverviewPan();
}

///////////////////////////////////////////////////////////////
// overview refresh *******************************************
// (refreshes the current position of the overview canvas)
///////////////////////////////////////////////////////////////
function OverviewPan()
{
    var Selection = MainRootElement.findName("Selection");
    var oLeft = OverviewLeftSpacing + (VisX / (OrigSizeX / OverviewSizeX) / Zoom());
    var oTop = OverviewTopSpacing + (VisY / (OrigSizeY / OverviewSizeY) / Zoom());
    Selection['Canvas.Left'] = oLeft;    
    Selection['canvas.top'] = oTop;
}

///////////////////////////////////////////////////////////////
// doubleclick directive **************************************
// (executes a zoom through a doubleclick)
///////////////////////////////////////////////////////////////
function DoubleClickZoom()
{    
    var sceneScale = MainRootElement.findName("sceneScale");
    sceneScale.centerX = DoubleClickX;
    sceneScale.centerY = DoubleClickY;
    ZoomIn(.05);
    
    if (Zoom() >= DoubleClickZoomEnd) 
    {
        ZoomStop(); 
    }
        
    OverviewZoom();
}

///////////////////////////////////////////////////////////////
// zoom in directive *******************************************
// (the global zooming function, takes speed as argument)
///////////////////////////////////////////////////////////////
function ZoomIn(speed)
{    
    if(CurrentKey == 17) speed = .5;
    if(speed == null) speed = .05;
    if(Zoom() < MaxDepth * ZoomLevel + 2 * ZoomLoad)
    {
        var sceneScale = MainRootElement.findName("sceneScale");
        sceneScale.scaleX += speed;
        sceneScale.scaleY += speed;        
    }
    else
    {
        ZoomStop(); 
    }
    CalculateEdges();
    OverviewZoom();
    processTileLogic();
}

///////////////////////////////////////////////////////////////
// zoom out directive *****************************************
// (the global zoom out function, takes speed as argument)
///////////////////////////////////////////////////////////////
function ZoomOut(speed)
{
    if(CurrentKey == 17) speed = .5;
    if(speed == null) speed = .05;  
    if (Zoom() <= 1) 
    {
        ResetScene(); 
        ZoomStop(); 
        ClearAndReset(true,true);               
    }
    else 
    {
        ClearAndReset(false,true);
        var sceneScale = MainRootElement.findName("sceneScale");
        sceneScale.scaleX -= speed;
        sceneScale.scaleY -= speed;
    }
    CalculateEdges();
    OverviewZoom();  
}

///////////////////////////////////////////////////////////////
// reset directive *****************************************
// (resets progressivly the current zoom and view to its original)
///////////////////////////////////////////////////////////////
function ResetAll()
{    
    ZoomOut(.1);
    
    if (Zoom() <= 1) 
    {
        ResetScene();
        ZoomStop(); 
    }
    
    CalculateEdges();    
    OverviewZoom();
}